package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.utils.DateUtil;
import pms.common.utils.PMSLog;
import pms.common.value.ThemeValue;

public class ThemeDaoImpl implements ThemeDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public List<ThemeValue> getThemeList(int projectId) {
		String sql="select * from tbl_theme where theme_project_id=? and del_fg=0 order by theme_id ";
		final List<ThemeValue> l_result = new ArrayList<ThemeValue>();
		jdbcTemplate.query(sql,new Object[]{new Integer(projectId)},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				ThemeValue l_tv = new ThemeValue();
				l_tv.setProjectId(rs.getInt("THEME_PROJECT_ID"));
				l_tv.setThemeId(rs.getInt("THEME_ID"));
				l_tv.setThemeName(rs.getString("THEME_NAME"));
				l_tv.setThemeProgress(rs.getInt("THEME_PROGRESS_ID"));
				l_tv.setThemeStatus(rs.getInt("THEME_STATUS"));
				l_tv.setAddTs(rs.getString("ADD_TS"));
				l_tv.setAddUserId(rs.getInt("ADD_USER_ID"));
				l_tv.setUpdTs(rs.getString("UPD_TS"));
				l_tv.setUpdUserId(rs.getInt("UPD_USER_ID"));
				l_tv.setDelTs(rs.getString("DEL_TS"));
				l_tv.setDelUserId(rs.getInt("DEL_USER_ID"));
				l_tv.setThemeStartDt(rs.getString("THEME_START_DT"));
				l_tv.setThemeEndDt(rs.getString("THEME_END_DT"));
				l_tv.setThemeInfo(rs.getString("THEME_INFO"));
				l_result.add(l_tv);
			}			
		});
		return l_result;
	}


	public void updateTheme(ThemeValue value) {
		PMSLog.start();
		String sql="update tbl_theme set theme_name=?, theme_status=?, theme_start_dt=?, theme_end_dt=?, theme_info=?, upd_user_id=?,upd_ts=? where theme_id=?";
		System.out.println(value.getThemeName());
		Object[] params = new Object[]{
				value.getThemeName(),
				value.getThemeStatus(),
				value.getThemeStartDt(),
				value.getThemeEndDt(),
				value.getThemeInfo(),
				value.getUpdUserId(),
				DateUtil.getCurrentTimestamp(),
				value.getThemeId()
				
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme update 0");
		} else {
			PMSLog.debug("theme updated");
		}
		PMSLog.end();
	}


	public void insertTheme(ThemeValue value) {
		PMSLog.start();
		String sql="insert into tbl_theme(theme_name,theme_status,theme_start_dt,theme_end_dt,theme_info,theme_project_id,add_user_id,add_ts) values(?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{
				value.getThemeName(),
				value.getThemeStatus(),
				value.getThemeStartDt(),
				value.getThemeEndDt(),
				value.getThemeInfo(),
				value.getProjectId(),
				value.getAddUserId(),
				DateUtil.getCurrentTimestamp()				
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme update 0");
		} else {
			PMSLog.debug("theme updated");
		}
		PMSLog.end();
		
	}


	public void deleteTheme(int themeId, int userId) {
		PMSLog.start();
		String sql="update tbl_theme set del_fg=1, del_user_id=?, del_ts=? where theme_id=?";
		Object[] params = new Object[]{
				userId,
				DateUtil.getCurrentTimestamp(),
				themeId
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme update 0");
		} else {
			PMSLog.debug("theme updated");
		}
		PMSLog.end();
		
	}

}
